﻿using LanguagePolish.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LanguagePolish.EntityModel
{
    partial class Class
        : BaseViewModelErrorInfo
    {
        /// <summary>
        /// Создает правила для валидации.
        /// </summary>
        public override void CreateRules()
        {
            AddRule("ClassName", () => string.IsNullOrWhiteSpace(ClassName), "'Class name' can't be empty!");
            AddRule("Duration", () => !Duration.HasValue, "'Duration' can't be empty!");
            AddRule("Duration", () => Duration.HasValue && Duration.Value <= 0, "'Duration' must be greater than zero!");
            AddRule("HoursPerDay", () => !HoursPerDay.HasValue, "'HoursPerDay' can't be empty!");
            AddRule("HoursPerDay", () => HoursPerDay.HasValue && HoursPerDay.Value <= 0, "'Hours Per Day' must be greater than zero!");
            AddRule("Capacity", () => !Capacity.HasValue, "'Capacity' can't be empty!");
            AddRule("Capacity", () => Capacity.HasValue && Capacity.Value <= 0, "'Capacity' must be greater than zero!");
            AddRule("Capacity", () => Classroom != null && Capacity.HasValue && Classroom.Capacity < Capacity, "Room capacity isn't enaught");
            AddRule("Classroom", () => Classroom != null && Capacity.HasValue && Classroom.Capacity < Capacity, "Room capacity isn't enaught");
        }

        public decimal? TotalHours 
        { 
            get 
            { 
                if (!HoursPerDay.HasValue || !Duration.HasValue)
                    return null;
                return HoursPerDay.Value * Duration.Value;
            }
        }

        public int StudentsCount
        {
            get { return Students.Count; }
        }

        public double? AverageGrade
        {
            get { return Lessons.SelectMany(l => l.Grades).Average(g => g.GradeValue); }
        }

        public double AttendanceRate
        {
            get { return 100.0; }
        }
    }
}
